package com.jd.controller;

import com.jd.common.Page;
import com.jd.pojo.Admin;
import com.jd.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AdminController {


    @Autowired
    private AdminService adminService;

    @RequestMapping("/admin/list")
    public String getUserList(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="10")Integer rows, String adminname, Model model) {

       Page<Admin> adminList =  adminService.selectAdminList(page,rows,adminname);
        model.addAttribute("page", adminList);
        model.addAttribute("adminname", adminname);
        return "adminManage";
    }


    @RequestMapping("/admin/add")
    @ResponseBody
    public String addAdmin(Admin admin) {
        adminService.addAdmin(admin);
        return "OK";
    }

    /**
     * 修改时用于回显
     * @param id
     * @return
     */
    @RequestMapping("/admin/edit")
    @ResponseBody
    public Admin getAdminById(Integer id) {
        Admin admin = adminService.getAdminById(id);
        return admin;
    }


    @RequestMapping("/admin/update")
    @ResponseBody
    public String updateAdmin(Admin admin) {
        adminService.updateAdmin(admin);
        return "OK";
    }

    @RequestMapping("/admin/delete")
    @ResponseBody
    public String deleteAdmin(Integer id) {
        adminService.deleteAdmin(id);
        return "OK";
    }

}
